package com.example.inter.tourdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.inter.tourdemo.ui.fragment.HomeFragment;
import com.example.inter.tourdemo.ui.fragment.MKTravelsFragment;
import com.example.inter.tourdemo.ui.fragment.MeFragment;
import com.example.inter.tourdemo.ui.fragment.SpecialtyFragment;
import com.example.inter.tourdemo.ui.fragment.ThemeFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.SupportFragment;

import static android.R.attr.id;

public class MainActivity extends SupportActivity {

    @Bind(R.id.fl_main_content)
    FrameLayout flMainContent;
    @Bind(R.id.iv_item_home)
    ImageView ivItemHome;
    @Bind(R.id.ll_item_home)
    LinearLayout llItemHome;
    @Bind(R.id.iv_item_invest)
    ImageView ivItemInvest;
    @Bind(R.id.ll_item_invest)
    LinearLayout llItemInvest;
    @Bind(R.id.iv_item_me)
    ImageView ivItemMe;
    @Bind(R.id.ll_item_me)
    LinearLayout llItemMe;
    @Bind(R.id.iv_item_more)
    ImageView ivItemMore;
    @Bind(R.id.ll_item_more)
    LinearLayout llItemMore;
    @Bind(R.id.iv_item_yj)
    ImageView ivItemYj;
    @Bind(R.id.ll_item_yj)
    LinearLayout llItemYj;

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOURTH = 3;
    public static final int FIVE = 4;

    private SupportFragment[] mFragments = new SupportFragment[5];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        if (savedInstanceState == null) {
            mFragments[FIRST] = HomeFragment.newInstance();
            mFragments[SECOND] = ThemeFragment.newInstance();
            mFragments[THIRD] = SpecialtyFragment.newInstance();
            mFragments[FOURTH] = MeFragment.newInstance();
            mFragments[FIVE] = MKTravelsFragment.newInstance();

            //把首页的按钮加载之前就表示选中.
            ivItemHome.setImageResource(R.drawable.bid01);
//            tvItemHome.setTextColor(getResources().getColor(R.color.title_text));
            loadMultipleRootFragment(R.id.fl_main_content, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD],
                    mFragments[FIVE],
                    mFragments[FOURTH]);

        }

        getIntent().putExtra("id",0);
        if(id == 0) {
            showHideFragment(mFragments[FIRST]);
        }

    }

    //设置点击监听
    @OnClick({R.id.ll_item_home, R.id.ll_item_invest, R.id.ll_item_me, R.id.ll_item_yj, R.id.ll_item_more})
    public void changeTab(View view) {
        switch (view.getId()) {
            case R.id.ll_item_home:
                SelectFragment(0);
                break;

            case R.id.ll_item_invest:
                SelectFragment(1);
                break;

            case R.id.ll_item_me:
                SelectFragment(2);
                break;
            case R.id.ll_item_yj:
                SelectFragment(3);
                break;

            case R.id.ll_item_more:
                SelectFragment(4);
                break;
        }
    }

    private void SelectFragment(int i) {

        //重新设置各个imageview和textview的显示
        resetTabColor();

        if (i == 0) {

            ivItemHome.setImageResource(R.drawable.bid01);
            showHideFragment(mFragments[FIRST]);
        } else if (i == 1) {
            showHideFragment(mFragments[SECOND]);
            ivItemInvest.setImageResource(R.drawable.bid03);
        } else if (i == 2) {
            showHideFragment(mFragments[THIRD]);
            ivItemMe.setImageResource(R.drawable.bid05);
        } else if (i == 3) {
            showHideFragment(mFragments[FIVE]);
            ivItemYj.setImageResource(R.drawable.bid07);
        } else if (i == 4) {
            showHideFragment(mFragments[FOURTH]);
            ivItemMore.setImageResource(R.drawable.bid09);
        }
    }

    //重新设置各个imageview和textview的显示
    private void resetTabColor() {
        //修改每一个imageview 和 每一个TextView
        ivItemHome.setImageResource(R.drawable.bid02);
        ivItemInvest.setImageResource(R.drawable.bid04);
        ivItemMe.setImageResource(R.drawable.bid06);
        ivItemMore.setImageResource(R.drawable.bid10);
        ivItemYj.setImageResource(R.drawable.bid08);

    }
}
